<template>
    <div class="mainDiv">
        <div class="rowHead">
            <el-button-group>
                <el-button type="success" @click="ruku" icon="h-kaidan" v-qx="permission.ru">入库</el-button>
                <el-button type="success" @click="chuku" icon="h-kaidan" v-qx="permission.chu">出库</el-button>
            </el-button-group>
            <el-select v-model="page.zt" @change="isjiezhang" class="ml w200" v-qx="permission.status">
                <el-option v-for="o in zhuanttai" :label="o.txt" :value="o.val" :key="o.val"></el-option>
            </el-select>
            <el-input style="width: 200px" placeholder="搜索单号、操作员" @keyup.enter.native="searchList" v-model="page.search">
                <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="searchList"></i>
            </el-input>
        </div>
        <el-table :data="list" :stripe="true" height="100%">
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
            <el-table-column prop="order_sn" label="单号"></el-table-column>
            <el-table-column prop="ordertype" label="出入类型">
                <template scope="scope">
                    <el-tag type="success" v-if="scope.row.ordertype == 6">入库</el-tag>
                    <el-tag type="warning" v-else>出库</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="addtime" label="开单日期">
                <template scope="scope">
                    {{time(scope.row.addtime)}}
                </template>
            </el-table-column>
            <el-table-column prop="aname" label="操作员"></el-table-column>
            <el-table-column prop="oname" label="审核员">
                <template scope="scope">{{scope.row.oname?scope.row.oname:'暂无'}}</template>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button-group v-if="scope.row.status==0">
                        <el-tooltip content="查看" placement="left" :open-delay="800" v-qx="permission.look">
                            <el-button type="info" size="mini" @click="chakan(scope.row.id)">查</el-button>
                        </el-tooltip>
                        <el-tooltip content="审核" placement="left" :open-delay="800" v-qx="permission.check">
                            <el-button type="warning" size="mini" @click="shenhe(scope.row.id)">审</el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top" :open-delay="800" v-qx="permission.del">
                            <el-button icon="el-icon-h-del" type="danger" size="mini" @click="shanchu(scope.row.id)"></el-button>
                        </el-tooltip>
                    </el-button-group>
                    <el-button type="info" icon="el-icon-h-chakan" size="mini" @click="chakan(scope.row.id)" v-else v-qx="permission.look"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="block center bg-light">
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :page-sizes="[15, 30, 50, 100]" :current-page="page.pages" :page-size="page.limits" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import C from '#/src/common/common.js'
export default {
    name: 'stockList',
    data() {
        return {
            list: [],
            total: 0,
            page: {
                pages: 1,
                limits: 15,
                search: '',
                zt: 0,
            },
            zhuanttai: [{
                txt: '未审核',
                val: 0
            }, {
                txt: '已审核',
                val: 1
            }],
            permission: {
                ru: 'orstock_ru',
                chu: 'orstock_chu',
                status: 'orstock_status',
                check: 'orstock_check',
                del: 'orstock_del',
                look: 'orstock_look',
            },
        }
    },
    mounted() {
        this.getList()
    },
    methods: {
        ruku: async function() {
            let res = await C.openBrowserWindow('/addStockorder', { ordertype: 6 }, {
                transparent: true,
                frame: false,
                width: 1000,
                height: 800,
                useSpa: true,
                spaModel: 'organization'
            })
            if (res) {
                C.success(res.data.info)
                this.getList()
            }
        },
        chuku: async function() {
            let res = await C.openBrowserWindow('/addStockorder', { ordertype: 7 }, {
                transparent: true,
                frame: false,
                width: 1000,
                height: 800,
                useSpa: true,
                spaModel: 'organization'
            })
            if (res) {
                C.success(res.data.info)
                this.getList()
            }
        },
        // 审核
        shenhe: async function(id) {
            let res = await C.openBrowserWindow('/stockOrderinfo', { id: id }, {
                transparent: true,
                frame: false,
                width: 1000,
                height: 800,
                useSpa: true,
                spaModel: 'organization'
            })
            if (res) {
                C.success(res.data.info)
                this.getList()
            }
        },
        // 删除订单
        shanchu: function(id) {
            let that = this
            C.confirm('删除当前订单, 是否继续?', async() => {
                let data = await C.ajax('/organization/stock/deletestockorder', { 'id': id }, 'post')
                if (data) {
                    C.success(data.data.info)
                    that.getList()
                }
            })
        },
        // 查看当前订单
        chakan: async function(id) {
            let res = await C.openBrowserWindow('/stockOrderinfo', { id: id }, {
                transparent: true,
                frame: false,
                width: 1000,
                height: 800,
                useSpa: true,
                spaModel: 'organization'
            })
            if (res) {
                C.success(res.data.info)
                this.getList()
            }
        },
        isjiezhang: function(val) {
            this.page.zt = val
            this.getList()
        },
        sizeChange: function(val) {
            this.page.limits = val
            this.getList()
        },
        currentChange: function(val) {
            this.page.pages = val
            this.getList()
        },
        searchList: function() {
            this.getList()
        },
        getList: async function(pages, limits, searchStr) {
            let that = this
            let url = '/organization/stock/orderlist'
            let data = await C.ajax(url, this.page, 'post')
            if (data) {
                that.list = data.data
                that.total = data.count
            }
        },
        time: function(unix) {
            let tm = new Date(unix * 1000)
            let yy = tm.getFullYear()
            let mm = tm.getMonth() < 9 ? '0' + (tm.getMonth() + 1) : tm.getMonth() + 1
            let dd = tm.getDate() < 10 ? '0' + tm.getDate() : tm.getDate()
            return yy + '-' + mm + '-' + dd
        },
    }
}
</script>